package main.java.content.substance;

import main.java.auxiliary.Constant;
import main.java.base.BaseElement;
import main.java.base.IElement;
import main.java.content.player.Player;

import java.awt.*;

/**
 * @author 吴浩华 陈逸然 吴畏
 * @Description: 地面
 * @date 2021/6/09
 */

@IElement("red.png")
public class Ground extends BaseElement{

    private int thickness = 40;

    public Ground(int x, int y) {
        super(x, y);
    }

    @Override
    public void drawImage(Graphics g) {
        int j =0;
        int i=0;
        int a = 1;
        //int i = Constant.FRAME_HEIGHT-25;//从屏幕最下面开始画
        j = (Constant.FRAME_HEIGHT-40-this.y)/40;
        while (i<= j+1) {

            switch (a){
                case 1:
                    g.drawImage(this.leftImage, this.x, Constant.FRAME_HEIGHT-i*40+10, 40, 40, null);
                    break;
                case 2:
                    g.drawImage(this.GImage2, this.x, Constant.FRAME_HEIGHT-i*40+10, 40, 40, null);
                    break;
                case 3:
                    g.drawImage(this.GImage3, this.x, Constant.FRAME_HEIGHT-i*40+10, 40, 40, null);
                    break;
                case 4:
                    g.drawImage(this.GImage4, this.x, Constant.FRAME_HEIGHT-i*40+10, 40, 40, null);
                    break;
            }
            i ++;
            a++;
            if(a>4){
                a = 1;
            }
        }
    }

    @Override
    public <E extends BaseElement> boolean aboveIntersects(E element) {
        Rectangle myself = new Rectangle(this.x, this.y, this.height-8, Constant.FRAME_HEIGHT-thickness);
        Rectangle be = element.getRectangle();
        return be.getY() < myself.getY() && myself.intersects(be);
    }

    @Override
    public Rectangle getRectangle() {
        //地面矩形范围 减去地面厚度 ，延申至地下
        return new Rectangle(this.x+3 , y + thickness, this.height-8, Constant.FRAME_HEIGHT);
    }

    /**
     * 跟随玩家移动
     *
     * @param player
     */
    public void movedByPlayer(Player player) {
        this.x -= player.getxSpeed();
    }
}
